<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="会议室名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入会议室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容纳人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入容纳人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地点" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column label="会议室名称" align="center" prop="roomName" />
      <el-table-column label="容纳人数" align="center" prop="capacity" />
      <el-table-column label="地点" align="center" prop="location" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="图片" align="center" prop="imagePath" >
        <template slot-scope="scope">
          <img :src="'http://localhost:8080' + scope.row.imagePath" alt="会议室图片" width="100%" height="150px">
        </template>
      </el-table-column>
      <el-table-column label="使用状态" align="center" prop="status" >
      <template v-slot="{ row }">
          <span v-if="row.status === 0">空闲</span>
          <span v-else-if="row.status === 1">使用中</span>
          <span v-else-if="row.status === 2">已预约</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['room:room:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReservation(scope.row)"
            v-hasPermi="['room:room:add']"
          >预约</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 会议室详情框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <div style="display: flex;">
      <div style="flex: 1;">
        <div style="width: 120%;">
          <img :src="'http://localhost:8080' + form.imagePath" alt="会议室图片" width="100%" height="150px" @click="showOverlay">
        </div>
      </div>
      <div style="flex: 1; padding-left: 100px;">
        <el-form ref="form" :model="form" :rules="rules" label-width="50px">
          <div>
            <span>会议室名称: {{ form.roomName }}</span>
          </div>
          <div>
            <span>容纳人数: {{ form.capacity }}</span>
          </div>
          <div>
            <span>地点: {{ form.location }}</span>
          </div>
          <div>
            <span>描述: {{ form.description }}</span>
          </div>
          <div>
            <span>会议室设备: {{ form.equipmentName }}</span>
          </div>
        </el-form>
      </div>
    </div>
    <div class="overlay" v-if="overlayVisible" @click="hideOverlay">
      <img :src="'http://localhost:8080' + form.imagePath" alt="Overlay Image">
    </div>
  </el-dialog>

  <!-- 预约对话框 -->
  <el-dialog :title="titleReservation" :visible.sync="openReservation" width="400px" append-to-body>  
  <el-form ref="form" :model="form" :rules="rules" label-width="120px">  
    <el-form-item label="预约日期" prop="date">  
      <el-date-picker  
        v-model="form.date"  
        type="date"  
        placeholder="请选择日期"  
        format="yyyy-MM-dd"  
        value-format="yyyy-MM-dd" :style="{ width: '200px' }"> 
      </el-date-picker>  
    </el-form-item>  
    <el-form-item label="开始时间" prop="start">  
      <el-time-picker
        v-model="form.start"
        :picker-options="{
          selectableRange: '09:00:00 - 18:00:00'
        }" format="HH:mm" value-format="HH:mm"
        placeholder="开始时间" :style="{ width: '200px' }">  
      </el-time-picker> 
    </el-form-item>  
    <el-form-item label="结束时间" prop="end">  
      <el-time-picker
        v-model="form.end"
        :picker-options="{
          selectableRange: '09:00:00 - 18:00:00'
        }" format="HH:mm" value-format="HH:mm"
        placeholder="结束时间" :style="{ width: '200px' }">
      </el-time-picker>   
    </el-form-item>  
    <el-form-item label="参与人员数量" prop="number">
      <el-input v-model="form.number" placeholder="请输入人员数量" :style="{ width: '200px' }"/> 
    </el-form-item>
  </el-form>  
  <div slot="footer" class="dialog-footer">  
    <el-button type="primary" @click="submitForm">确 定</el-button>  
    <el-button @click="cancel">取 消</el-button>  
  </div>  
</el-dialog> 
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom, getEquipmentName } from "@/api/room/room";
import { addReservation } from "@/api/room/reservation";

export default {
  name: "Room",
  data() {
    return {
      start: new Date(),
      end: new Date(),
      overlayVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议室表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      titleReservation:"",
      // 是否显示弹出层
      open: false,
      // 是否显示预约按钮弹出层
      openReservation: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomName: null,
        capacity: null,
        location: null,
        description: null,
        status: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        start: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        end: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "参与人数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showOverlay() {
      this.overlayVisible = true;
    },
    hideOverlay() {
      this.overlayVisible = false;
    },
    /** 查询会议室列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openReservation = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        roomName: null,
        capacity: null,
        location: null,
        description: null,
        delFlag: null,
        status: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail(row){
      this.reset();
      const roomId = row.roomId
      getEquipmentName(roomId).then(response =>{
        this.form = response.data;
        this.open = true;
        this.title = "会议室详情";
      });
      },
    /** 预约按钮操作 */
    handleReservation(row) {
      this.reset();
      const roomId = row.roomId
      getRoom(roomId).then(response => {
        this.form = response.data;
        this.openReservation = true;
        this.titleReservation = "预约会议室";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
           // 拼接日期和时间  
          const startTimeStr = `${this.form.date} ${this.form.start}:00`;  
          const endTimeStr = `${this.form.date} ${this.form.end}:00`;  
          // 将字符串转换为 Date 对象  
          const startTime = new Date(startTimeStr);  
          const endTime = new Date(endTimeStr);  
          // 对开始时间和结束时间进行验证  
          if (endTime < startTime) {  
            this.$message.error('结束时间必须晚于开始时间！');  
            return; // 如果时间无效，直接返回，不再执行后续操作  
          }  
          // 验证通过，将拼接后的时间赋值给表单数据  
          this.form.startTime = startTimeStr;  
          this.form.endTime = endTimeStr; 
          addReservation(this.form).then(response => {
            this.$modal.msgSuccess("预约信息添加成功，请等待审核");
            this.openReservation = false; // 关闭对话框 
            this.getList();
          });
        }else {
            console.log('表单验证失败！');  
            return false;  
          }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roomId = row.roomId;
      this.$modal.confirm('是否确认删除会议室编号为"' + roomId + '"的数据项？').then(function() {
        return delRoom(roomId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('room/room/export', {
        ...this.queryParams
      }, `room_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.overlay {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
  z-index: 999;
}

.overlay img {
  max-width: 90%;
  max-height: 90%;
}
</style>
